import { create } from "zustand";

interface ChatSessionPageState {
  state: {
    enableMultipleSelect: boolean;
    selectedSessions: string[];
    selectCount: number;
  };
  actions: {
    changeMultipleSelectMode: (multipleSelectMode: boolean) => void;
    addSelectedSession: (sessionId: string) => void;
    removeSelectedSession: (sessionId: string) => void;
  };
}

const ChatSessionPageStateStore = create<ChatSessionPageState>((set, get) => ({
  state: {
    enableMultipleSelect: false,
    selectedSessions: [],
    selectCount: 0,
  },
  actions: {
    changeMultipleSelectMode: (enableMultipleSelect: boolean) => {
      set((state) => ({
        state: {
          ...state.state,
          enableMultipleSelect,
          selectedSessions: !enableMultipleSelect
            ? []
            : state.state.selectedSessions,
          selectCount: !enableMultipleSelect ? 0 : state.state.selectCount,
        },
      }));
    },
    addSelectedSession: (sessionId: string) => {
      set((state) => {
        const selectedSessions = [...state.state.selectedSessions, sessionId];
        return {
          state: {
            ...state.state,
            selectedSessions,
            selectCount: selectedSessions.length,
          },
        };
      });
    },
    removeSelectedSession: (sessionId: string) => {
      set((state) => {
        const selectedSessions = state.state.selectedSessions.filter(
          (id) => id !== sessionId
        );
        return {
          state: {
            ...state.state,
            selectedSessions,
            selectCount: selectedSessions.length,
          },
        };
      });
    },
  },
}));

export default ChatSessionPageStateStore;
